/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPencatatanPeminjaman1.java
 *
 * Created on Nov 12, 2011, 11:02:21 AM
 */
package Pinjam_Kembali;

//import Entity.Peminjaman;
import Entity.Peminjaman;
import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author wienda
 */
public class FormPencatatanPeminjaman extends javax.swing.JPanel {

    /** Creates new form FormPencatatanPeminjaman1 */
    public FormPencatatanPeminjaman() {
        initComponents();
        GregorianCalendar gc = new GregorianCalendar();
        tanggalchooser.setDate(gc.getTime());
        tanggalchooser.setEnabled(false);
        buttonGroup1.add(jRadioButton1);
        buttonGroup1.add(jRadioButton2);
        buttonGroup1.add(jRadioButton3);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        idBukuLabel = new javax.swing.JLabel();
        idPeminjamLabel = new javax.swing.JLabel();
        idMemberText = new javax.swing.JTextField();
        tanggalchooser = new com.toedter.calendar.JDateChooser();
        menuBox = new javax.swing.JComboBox();
        judul = new javax.swing.JLabel();
        simpanButton = new javax.swing.JButton();
        idBukuText0 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        idBukuText1 = new javax.swing.JTextField();
        idBukuText2 = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();

        setBackground(new java.awt.Color(102, 102, 255));

        idBukuLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        idBukuLabel.setText("Id Buku");

        idPeminjamLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        idPeminjamLabel.setText("Id Peminjam");

        menuBox.setFont(new java.awt.Font("Tahoma", 1, 12));
        menuBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Peminjaman", "Pengembalian" }));
        menuBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuBoxActionPerformed(evt);
            }
        });

        judul.setFont(new java.awt.Font("Stencil", 0, 20));
        judul.setText("Pencatatan Peminjaman");

        simpanButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        idBukuText1.setEnabled(false);
        idBukuText1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idBukuText1ActionPerformed(evt);
            }
        });

        idBukuText2.setEnabled(false);

        jRadioButton1.setBackground(new java.awt.Color(102, 102, 255));
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("1");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setBackground(new java.awt.Color(102, 102, 255));
        jRadioButton2.setText("2");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jRadioButton3.setBackground(new java.awt.Color(102, 102, 255));
        jRadioButton3.setText("3");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addComponent(judul)
                .addContainerGap(157, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(menuBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton3)
                        .addComponent(jRadioButton1, javax.swing.GroupLayout.Alignment.TRAILING)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(idBukuLabel)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(idPeminjamLabel)
                                        .addGap(18, 18, 18)
                                        .addComponent(idMemberText, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(idBukuText0, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(idBukuText1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tanggalchooser, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(idBukuText2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(156, 156, 156)
                        .addComponent(simpanButton)))
                .addContainerGap(29, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(judul, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(menuBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addComponent(jRadioButton1)
                        .addGap(6, 6, 6)
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton3))
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tanggalchooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(idMemberText, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(idPeminjamLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(idBukuLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(idBukuText0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(idBukuText1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(idBukuText2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46)
                        .addComponent(simpanButton)))
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("");
    }// </editor-fold>//GEN-END:initComponents

private void menuBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuBoxActionPerformed
    this.judul.setText("Pencatatan " + this.menuBox.getSelectedItem());
    // TODO add your handling code here:
}//GEN-LAST:event_menuBoxActionPerformed

    void kosongin() {
        idMemberText.setText("");
        idBukuText0.setText("");
        idBukuText1.setText("");
        idBukuText2.setText("");
    }

private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed
    try {
        String idbuku = idBukuText0.getText();
        String idmember = idMemberText.getText();
        Date tanggal = tanggalchooser.getDate();

        Peminjaman p = new Peminjaman();
        p.setIdPeminjam(idmember);
        p.setIdBuku(idbuku);

        if (((String) menuBox.getSelectedItem()).equalsIgnoreCase("Peminjaman")) {

            p.setTanggal(tanggal);
            p.simpanPeminjaman();
            p.updateStock(-1);
        } else {
            p.setTanggalKembali(tanggal);
            p.simpanPengembalian();
            p.updateStock(1);
        }
        JOptionPane.showMessageDialog(this, "Data " + menuBox.getSelectedItem() + idbuku + " berhasil disimpan");

        if (idBukuText1.isEnabled()) {
            idbuku = idBukuText1.getText();

            p.setIdBuku(idbuku);

            if (((String) menuBox.getSelectedItem()).equalsIgnoreCase("Peminjaman")) {
                p.setTanggal(tanggal);
                p.simpanPeminjaman();
                p.updateStock(-1);
            } else {
                p.setTanggalKembali(tanggal);
                p.simpanPengembalian();
                p.updateStock(1);
            }
        }
        JOptionPane.showMessageDialog(this, "Data " + menuBox.getSelectedItem() + idbuku + " berhasil disimpan");

        if (idBukuText2.isEnabled()) {
            idbuku = idBukuText2.getText();

            p.setIdBuku(idbuku);

            if (((String) menuBox.getSelectedItem()).equalsIgnoreCase("Peminjaman")) {
                p.setTanggal(tanggal);
                p.simpanPeminjaman();
                p.updateStock(-1);
            } else {
                p.setTanggalKembali(tanggal);
                p.simpanPengembalian();
                p.updateStock(1);
            }
        }
        JOptionPane.showMessageDialog(this, "Data " + menuBox.getSelectedItem() + idbuku + " berhasil disimpan");
        kosongin();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Data gagal ditambahkan cek semua kode");
        Logger.getLogger(FormPencatatanPeminjaman.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(FormPencatatanPeminjaman.class.getName()).log(Level.SEVERE, null, ex);
    }
    kosongin();
}//GEN-LAST:event_simpanButtonActionPerformed

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
    idBukuText0.setEnabled(true);
    idBukuText1.setEnabled(false);
    idBukuText2.setEnabled(false);
    idBukuText1.setText(null);
    idBukuText2.setText(null);
}//GEN-LAST:event_jRadioButton1ActionPerformed

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
    idBukuText0.setEnabled(true);
    idBukuText1.setEnabled(true);
    idBukuText2.setEnabled(false);
    idBukuText2.setText(null);
}//GEN-LAST:event_jRadioButton2ActionPerformed

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
    idBukuText0.setEnabled(true);
    idBukuText1.setEnabled(true);
    idBukuText2.setEnabled(true);
}//GEN-LAST:event_jRadioButton3ActionPerformed

private void idBukuText1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idBukuText1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_idBukuText1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel idBukuLabel;
    private javax.swing.JTextField idBukuText0;
    private javax.swing.JTextField idBukuText1;
    private javax.swing.JTextField idBukuText2;
    private javax.swing.JTextField idMemberText;
    private javax.swing.JLabel idPeminjamLabel;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel judul;
    private javax.swing.JComboBox menuBox;
    private javax.swing.JButton simpanButton;
    private com.toedter.calendar.JDateChooser tanggalchooser;
    // End of variables declaration//GEN-END:variables
}
